<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是编译与调试——GCC基础的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-03-编译与调试-01-GCC基础">
<meta property="og:url" content="https://sumumm.github.io/post/b065a412.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是编译与调试——GCC基础的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2024-10-30T00:00:11.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.988Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV02-编译基础">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://sumumm.github.io/post/b065a412.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":"","permalink":"https://sumumm.github.io/post/b065a412.html","path":"post/b065a412.html","title":"LV02-03-编译与调试-01-GCC基础"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-03-编译与调试-01-GCC基础 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81-GCC-%E7%AE%80%E4%BB%8B"><span class="nav-text">一、 GCC 简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-GNU%EF%BC%9F"><span class="nav-text">1. GNU？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-GCC%E7%BC%96%E8%AF%91%E5%B7%A5%E5%85%B7%E9%93%BE"><span class="nav-text">2. GCC编译工具链</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-GCC%E7%BC%96%E8%AF%91%E5%99%A8%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="nav-text">3. GCC编译器是什么？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-GCC%E7%89%88%E6%9C%AC%E6%9F%A5%E7%9C%8B"><span class="nav-text">4. GCC版本查看</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-GCC-%E5%AE%89%E8%A3%85%E4%BD%8D%E7%BD%AE"><span class="nav-text">5. GCC 安装位置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-Binutils%E5%B7%A5%E5%85%B7%E9%9B%86"><span class="nav-text">6. Binutils工具集</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-%E5%B7%A5%E5%85%B7%E7%AE%80%E4%BB%8B"><span class="nav-text">6.1 工具简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-%E8%BF%99%E4%BA%9B%E5%B7%A5%E5%85%B7%E9%83%BD%E5%9C%A8%E5%93%AA%EF%BC%9F"><span class="nav-text">6.2 这些工具都在哪？</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-glibc%E5%BA%93"><span class="nav-text">7. glibc库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-1-%E5%BA%93%E7%AE%80%E4%BB%8B"><span class="nav-text">7.1 库简介</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-2-%E6%BA%90%E7%A0%81"><span class="nav-text">7.2 源码</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E7%BC%96%E8%AF%91%E5%99%A8"><span class="nav-text">二、编译器</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E7%BC%96%E8%AF%91%E5%99%A8"><span class="nav-text">1. 什么是编译器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E7%BC%96%E8%AF%91%E5%99%A8%E7%94%B1%E5%93%AA%E4%BA%9B%E7%BB%84%E6%88%90%EF%BC%9F"><span class="nav-text">2. 编译器由哪些组成？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E7%BC%96%E8%AF%91%E5%99%A8%E7%9A%84%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF"><span class="nav-text">4. 编译器的核心技术</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/b065a412.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-03-编译与调试-01-GCC基础 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-03-编译与调试-01-GCC基础
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-10-30 08:00:11" itemprop="dateCreated datePublished" datetime="2024-10-30T08:00:11+08:00">2024-10-30</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV02-编译基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>3.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>14 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是编译与调试——GCC基础的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-03-%E7%BC%96%E8%AF%91%E4%B8%8E%E8%B0%83%E8%AF%95-01-GCC%E5%9F%BA%E7%A1%80/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <p>说明：此篇笔记若有更新，系统环境可能会发生变化，但大概的应该都是一样的。</p><table>    <tr>        <td align="center" rowspan="5">PC端开发环境</td>        <td align="center" width=150px>Windows</td>        <td align="left">Windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu20.04.6的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 17 Pro</td>        <td align="left">17.0.0 build-20800274</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>    </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr>    <tr>        <td align="center" rowspan="3">Linux开发板环境</td>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <ul><li>通用</li></ul><table>    <tr><td align="center">参考资料  </td><td align="center">相关链接</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">---<i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr><td align="left"><a href="" target="_blank">---</a></td><td align="left">---</td></tr></table>
              </div>
            </details>

<h1 id="一、-GCC-简介"><a href="#一、-GCC-简介" class="headerlink" title="一、 GCC 简介"></a><font size=3>一、 GCC 简介</font></h1><h2 id="1-GNU？"><a href="#1-GNU？" class="headerlink" title="1. GNU？"></a><font size=3>1. GNU？</font></h2><p>我们先来看一下<a target="_blank" rel="noopener" href="https://www.gnu.org/">GNU</a>，官网是这么说的：</p>
<blockquote>
<p>GNU 是一个 <a target="_blank" rel="noopener" href="https://www.gnu.org/philosophy/free-sw.html">自由软件</a> 操作系统—就是说，它尊重其使用者的自由。GNU 操作系统包括 GNU 软件包（专门由 GNU 工程发布的程序）和由第三方发布的自由软件。GNU 的开发使你能够使用电脑而无需安装可能会侵害你自由的软件。</p>
<p>GNU 是一个类 Unix 操作系统。它是由多个应用程序、系统库、开发工具乃至游戏构成的程序集合。GNU 的开发始于 1984 年 1 月，称为 GNU 工程。GNU 的许多程序在 GNU 工程下发布；我们称之为 <a target="_blank" rel="noopener" href="https://www.gnu.org/software/">GNU 软件包</a>。</p>
</blockquote>
<ul>
<li><strong>自由软件运动</strong></li>
</ul>
<p>自由软件运动致力于通过自由软件使计算机用户获得自由计算的权利。自由软件的用户可以自主控制自己的计算。非自由软件使用户受制于软件开发者。现在自由软件运动远远不仅是开发 GNU 系统。</p>
<ul>
<li><strong>自由软件意味着使用者有运行、复制、发布、研究、修改和改进该软件的自由。</strong></li>
</ul>
<p>自由软件关乎自由，而非价格。要理解这个概念，你应该考虑 “free” 是 “言论自由（free speech）”中的“自由”；而不是 “免费啤酒（free beer）”中的“免费”。</p>
<p>更精确地说，自由软件赋予软件使用者<a target="_blank" rel="noopener" href="https://www.gnu.org/philosophy/free-sw.html">四项基本自由</a>：</p>
<p>（1）不论目的为何，有运行该软件的自由（自由之零）。</p>
<p>（2）有研究该软件如何工作以及按需改写该软件的自由（自由之一）。取得该软件源代码为达成此目的之前提。</p>
<p>（3）有重新发布拷贝的自由，这样你可以借此来敦亲睦邻（自由之二）。</p>
<p>（4）有向公众发布改进版软件的自由（自由之三），这样整个社群都可因此受惠。取得该软件源码为达成此目的之前提。</p>
<h2 id="2-GCC编译工具链"><a href="#2-GCC编译工具链" class="headerlink" title="2. GCC编译工具链"></a><font size=3>2. GCC编译工具链</font></h2><p>GCC编译工具链（toolchain）是指以GCC编译器为核心的一整套工具，用于把源代码转化成可执行应用程序。它主要包含以下三部分内容：</p>
<ul>
<li>gcc-core：即GCC编译器，用于完成预处理和编译过程，例如把C代码转换成汇编代码。</li>
<li>Binutils ：除GCC编译器外的一系列小工具包括了链接器ld，汇编器as、目标文件格式查看器readelf等。</li>
<li>glibc：包含了主要的 C语言标准函数库，C语言中常常使用的打印函数printf、malloc函数就在glibc 库中。</li>
</ul>
<p>在很多场合下会直接用GCC编译器来指代整套GCC编译工具链。</p>
<h2 id="3-GCC编译器是什么？"><a href="#3-GCC编译器是什么？" class="headerlink" title="3. GCC编译器是什么？"></a><font size=3>3. GCC编译器是什么？</font></h2><p><a target="_blank" rel="noopener" href="http://gcc.gnu.org/"><strong>GCC</strong></a> 全称为 GNU CC  ， GNU 项目中符合 ANSI C 标准的编译系统 ，可以编译如 C 、 C++ 、 Object C 、 Java 、 Fortran 、 Pascal 、 Modula-3 和 Ada 等多种语言。</p>
<p>它是可以在多种硬体平台上编译出可执行程序的超级编译器，其执行效率与一般的编译器相比平均效率要高 20%~30% 。它是一个交叉平台编译器 ，适合在嵌入式领域进行开发编译。GCC 还能运行在不同的操作系统上，如 Linux 、 Solaris 、 Windows 等。</p>
<p>GCC 支持的文件后缀名解释如下：</p>
<table>
    <tr><td align="center" width=100px>.c</td><td align="left">C原始程序</td></tr>
    <tr><td align="center" width=100px>.C/.cc/.cxx	</td><td align="left">C++原始程序</td></tr>
    <tr><td align="center" width=100px>.h</td><td align="left">预处理文件(头文件)</td></tr>
    <tr><td align="center" width=100px>.i</td><td align="left">已经过预处理的C原始程序</td></tr>
    <tr><td align="center" width=100px>.ii</td><td align="left">已经过预处理的C++原始程序</td></tr>
    <tr><td align="center" width=100px>.s/.S</td><td align="left">汇编语言原始程序</td></tr>
    <tr><td align="center" width=100px>.o</td><td align="left">目标文件</td></tr>
    <tr><td align="center" width=100px>.a/.so</td><td align="left">编译后的库文件</td></tr>
    <tr><td align="center" width=100px>.m</td><td align="left">Objective-C源代码文件</td></tr>
</table>


<h2 id="4-GCC版本查看"><a href="#4-GCC版本查看" class="headerlink" title="4. GCC版本查看"></a><font size=3>4. GCC版本查看</font></h2><p>在Ubuntu系统下系统默认已经安装好GCC编译器（若是没安装，也可以直接使用apt-get等相关命令安装），可以通过如下命令查看Ubuntu系统中GCC编译器的版本及安装路径：</p>
<ul>
<li>gcc –version</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sumu@sumu-virtual-machine:~$ gcc --version</span><br><span class="line">gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0</span><br><span class="line">Copyright (C) 2019 Free Software Foundation, Inc.</span><br><span class="line">This is free software; see the source for copying conditions.  There is NO</span><br><span class="line">warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.</span><br></pre></td></tr></table></figure>

<ul>
<li>gcc -v</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">sumu@sumu-virtual-machine:~$ gcc -v</span><br><span class="line">Using built-in specs.</span><br><span class="line">COLLECT_GCC=gcc</span><br><span class="line">COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper</span><br><span class="line">OFFLOAD_TARGET_NAMES=nvptx-none:hsa</span><br><span class="line">OFFLOAD_TARGET_DEFAULT=1</span><br><span class="line">Target: x86_64-linux-gnu</span><br><span class="line">Configured with: ../src/configure -v --with-pkgversion=&#x27;Ubuntu 9.4.0-1ubuntu1~20.04.2&#x27; --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-9QDOt0/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu</span><br><span class="line">Thread model: posix</span><br><span class="line">gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2) </span><br></pre></td></tr></table></figure>

<p>这个命令的显示信息更详细：</p>
<p>（1）“Target: x86_64-linux-gnu”表示该GCC的目标平台为x86_64架构（Intel、AMD的CPU）， 表示它编译生成的应用程序只适用于x86架构，不适用于ARM开发板平台。</p>
<p>（2）“gcc version 9.4.0”表明该GCC的版本为9.4.0，部分程序可能会对编译器版本有要求， 像编译指定版本的uboot、Linux内核的时候可能会对GCC有版本要求。</p>
<h2 id="5-GCC-安装位置"><a href="#5-GCC-安装位置" class="headerlink" title="5. GCC 安装位置"></a><font size=3>5. GCC 安装位置</font></h2><ul>
<li>which gcc</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sumu@sumu-virtual-machine:~$ which gcc</span><br><span class="line">/usr/bin/gcc</span><br></pre></td></tr></table></figure>

<h2 id="6-Binutils工具集"><a href="#6-Binutils工具集" class="headerlink" title="6. Binutils工具集"></a><font size=3>6. Binutils工具集</font></h2><h3 id="6-1-工具简介"><a href="#6-1-工具简介" class="headerlink" title="6.1 工具简介"></a><font size=3>6.1 工具简介</font></h3><p>Binutils（bin utility），是GNU二进制工具集，通常跟GCC编译器一起打包安装到系统，它的官方说明网站地址为：<a target="_blank" rel="noopener" href="https://www.gnu.org/software/binutils/">Binutils - GNU Project - Free Software Foundation</a></p>
<p>在进行程序开发的时候通常不会直接调用这些工具，而是在使用GCC编译指令的时候由GCC编译器间接调用。下面是其中一些常用的工具：</p>
<ul>
<li>as：汇编器，把汇编语言代码转换为机器码（目标文件）。</li>
<li>ld：链接器，把编译生成的多个目标文件组织成最终的可执行程序文件。</li>
<li>readelf：可用于查看目标文件或可执行程序文件的信息。</li>
<li>nm ： 可用于查看目标文件中出现的符号。</li>
<li>objcopy： 可用于目标文件格式转换，如.bin 转换成 .elf 、.elf 转换成 .bin等。</li>
<li>objdump：可用于查看目标文件的信息，最主要的作用是反汇编。</li>
<li>size：可用于查看目标文件不同部分的尺寸和总尺寸，例如代码段大小、数据段大小、使用的静态内存、总大小等。</li>
<li><strong>addr2line</strong>：将地址转换为文件名和行号。这个命令还是很有用的，经常可以用于段错误的排查。</li>
</ul>
<h3 id="6-2-这些工具都在哪？"><a href="#6-2-这些工具都在哪？" class="headerlink" title="6.2 这些工具都在哪？"></a><font size=3>6.2 这些工具都在哪？</font></h3><p>系统默认的Binutils工具集位于&#x2F;usr&#x2F;bin目录下，可使用如下命令查看系统中存在的Binutils工具集：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">sumu@sumu-virtual-machine:~$ ls /usr/bin/ | grep linux-gnu-</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">...</span></span><br><span class="line">x86_64-linux-gnu-addr2line</span><br><span class="line">x86_64-linux-gnu-ar</span><br><span class="line">x86_64-linux-gnu-as</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">...</span></span><br><span class="line">x86_64-linux-gnu-gcc</span><br><span class="line">x86_64-linux-gnu-gcc-9</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">...</span></span><br><span class="line">x86_64-linux-gnu-ld</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">...</span></span><br><span class="line">x86_64-linux-gnu-objcopy</span><br><span class="line">x86_64-linux-gnu-objdump</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">...</span></span><br><span class="line">x86_64-linux-gnu-readelf</span><br><span class="line">x86_64-linux-gnu-size</span><br><span class="line">x86_64-linux-gnu-strings</span><br><span class="line">x86_64-linux-gnu-strip</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">...</span></span><br></pre></td></tr></table></figure>

<h2 id="7-glibc库"><a href="#7-glibc库" class="headerlink" title="7. glibc库"></a><font size=3>7. glibc库</font></h2><h3 id="7-1-库简介"><a href="#7-1-库简介" class="headerlink" title="7.1 库简介"></a><font size=3>7.1 库简介</font></h3><p>glibc库是GNU组织为GNU系统以及Linux系统编写的C语言标准库，因为绝大部分C程序都依赖该函数库，该文件甚至会直接影响到系统的正常运行，例如常用的文件操作函数read、write、open，打印函数printf、动态内存申请函数malloc等。</p>
<p>在Ubuntu系统下，libc.so.6是glibc的库文件，可直接执行该库文件查看版本，在主机上执行如下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">sumu@sumu-virtual-machine:~$ /lib/x86_64-linux-gnu/libc.so.6</span><br><span class="line">GNU C Library (Ubuntu GLIBC 2.31-0ubuntu9.16) stable release version 2.31.</span><br><span class="line">Copyright (C) 2020 Free Software Foundation, Inc.</span><br><span class="line">This is free software; see the source for copying conditions.</span><br><span class="line">There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A</span><br><span class="line">PARTICULAR PURPOSE.</span><br><span class="line">Compiled by GNU CC version 9.4.0.</span><br><span class="line">libc ABIs: UNIQUE IFUNC ABSOLUTE</span><br><span class="line">For bug reporting instructions, please see:</span><br><span class="line">&lt;https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs&gt;.</span><br></pre></td></tr></table></figure>

<p>表示本系统中使用的glibc是2.31版本，是由GCC 9.4.0版本的编译器编译出来的。</p>
<h3 id="7-2-源码"><a href="#7-2-源码" class="headerlink" title="7.2 源码"></a><font size=3>7.2 源码</font></h3><p>学习C语言的时候，可能特别好奇printf、malloc之类的函数是如何实现的， 但是在Windows下的C库是不开源的，无法查看，而在Linux下， 则可以直接研究glibc的源代码，甚至加入开发社区贡献自己的代码，glibc的官网地址为： <a target="_blank" rel="noopener" href="https://www.gnu.org/software/libc/">The GNU C Library - GNU Project - Free Software Foundation</a>，可在该网站中下载源代码来学习。</p>
<h1 id="二、编译器"><a href="#二、编译器" class="headerlink" title="二、编译器"></a><font size=3>二、编译器</font></h1><p>了解了那么多GCC的相关基础知识，那么什么是编译器呢？</p>
<h2 id="1-什么是编译器"><a href="#1-什么是编译器" class="headerlink" title="1. 什么是编译器"></a><font size=3>1. 什么是编译器</font></h2><p>编译器，作为计算机科学与软件工程的核心工具，<strong>将人类可读的高级语言源代码转换成机器可执行的低级指令</strong>。这一复杂的过程涉及多个阶段，包括词法分析、语法分析、语义分析、优化和代码生成等。</p>
<h2 id="2-编译器由哪些组成？"><a href="#2-编译器由哪些组成？" class="headerlink" title="2. 编译器由哪些组成？"></a><font size=3>2. 编译器由哪些组成？</font></h2><p>一个编译器主要的组成部分如下所示：</p>
<table>
    <tr><td align="center" width=100px>分析器 </td><td align="left">分析器将源语言程序代码转换为汇编语言。因为要从一种格式转换为另一种格式（C到汇编），所以分析器需要知道目标机器的汇编语言。</td></tr>
    <tr><td align="center" width=100px>汇编器 </td><td align="left">汇编器将汇编语言代码转换为CPU可以执行字节码。</td></tr>
    <tr><td align="center" width=100px>链接器 </td><td align="left">链接器将汇编器生成的单独的目标文件组合成可执行的应用程序。链接器需要知道这种目标格式以便工作。</td></tr>
    <tr><td align="center" width=100px>标准C库</td><td align="left">核心的C函数都有一个主要的C库来提供。如果在应用程序中用到了C库中的函数，这个库就会通过链接器和源代码连接来生成最终的可执行程序。</td></tr>
</table>
## <font size=3>3. 编译器要做哪些事？</font>

<p>编译器主要由前端和后端两大部分组成。前端负责处理源代码，包括词法分析、语法分析和语义分析，生成中间表示（IR）。后端则根据中间表示生成目标代码，包括优化和代码生成等步骤。</p>
<h2 id="4-编译器的核心技术"><a href="#4-编译器的核心技术" class="headerlink" title="4. 编译器的核心技术"></a><font size=3>4. 编译器的核心技术</font></h2><p>（1）词法分析与正则表达式：编译器使用正则表达式或有限自动机进行词法分析，识别源代码中的不同成分。</p>
<p>（2）语法分析与解析算法：编译器通常采用递归下降解析或基于上下文的无关文法（CFG）进行语法分析，构建AST（抽象语法树）。</p>
<blockquote>
<p>抽象语法树（abstract syntax code，AST）是源代码的抽象语法结构的树状表示，树上的每个节点都表示源代码中的一种结构，这所以说是抽象的，是因为抽象语法树并不会表示出真实语法出现的每一个细节，比如说，嵌套括号被隐含在树的结构中，并没有以节点的形式呈现。抽象语法树并不依赖于源语言的语法，也就是说语法分析阶段所采用的上下文无文文法，因为在写文法时，经常会对文法进行等价的转换（消除左递归，回溯，二义性等），这样会给文法分析引入一些多余的成分，对后续阶段造成不利影响，甚至会使合个阶段变得混乱。因些，很多编译器经常要独立地构造语法分析树，为前端，后端建立一个清晰的接口。</p>
<p>抽象语法树在很多领域有广泛的应用，比如浏览器，智能编辑器，编译器。</p>
</blockquote>
<p>（3）语义分析与类型系统：编译器通过检查类型、作用域、符号表等来确保源代码的语义正确性。 </p>
<p>（4）中间表示与优化技术：编译器使用SSA（静态单一赋值）形式、控制流图等中间表示，并采用各种优化技术如常量折叠、死代码消除、循环展开等来提高代码质量。 </p>
<p>（5）目标代码生成与机器模型：编译器根据目标机器的指令集和特性生成高效的机器代码。</p>
<blockquote>
<p>参考资料：</p>
<p><a target="_blank" rel="noopener" href="https://developer.aliyun.com/article/1523509">编译器：原理与技术的奥秘-阿里云开发者社区 (aliyun.com)</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/b065a412.html">LV02-03-编译与调试-01-GCC基础</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年10月30日 - 08:00</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/b065a412.html" title="LV02-03-编译与调试-01-GCC基础">https://sumumm.github.io/post/b065a412.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV02-编译基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/f7a56c0b.html" rel="prev" title="LV02-03-编译与调试-02-GCC编译">
                  <i class="fa fa-angle-left"></i> LV02-03-编译与调试-02-GCC编译
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/c145821.html" rel="next" title="LV29-01-网络编程-03-socket编程实例">
                  LV29-01-网络编程-03-socket编程实例 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
